package com.lv.common.utils

import android.Manifest
import android.app.Activity
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.lv.common.dialog.PermissionXCustomDialog
import com.permissionx.guolindev.PermissionX

/**
 * 作者： ygx
 * 创建日期：2023/2/16 3:20 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：PermissionX 封装
 */
object PermissionXUtils {
    /**
     * 先提醒用户要申请权限的目的(activity)
     */
    fun checkPermissBefore(
        activity: FragmentActivity,
        permissions: List<String>,
        hint: String = "即将重新申请的权限是程序必须依赖的权限",
        callback: PermissionXUtilsCallback
    ) {
        PermissionX.init(activity)
            .permissions(
                permissions
            )
            .explainReasonBeforeRequest()
            .onExplainRequestReason { scope, deniedList ->
                val permissionXCustomDialog =
                    PermissionXCustomDialog(activity, hint, deniedList)
                permissionXCustomDialog.setCancelable(true)
                permissionXCustomDialog.setCanceledOnTouchOutside(true)
                scope.showRequestReasonDialog(permissionXCustomDialog)
            }.onForwardToSettings { scope, deniedList ->
                val permissionXCustomDialog =
                    PermissionXCustomDialog(activity, "您需要去应用程序设置当中手动开启权限", deniedList)
                scope.showForwardToSettingsDialog(permissionXCustomDialog)
            }.request { allGranted, grantedList, deniedList ->
                callback.permissionCallback(allGranted, grantedList, deniedList)
            }
    }

    /**
     * 直接申请权限（activity)
     */
    fun checkPermiss(
        activity: FragmentActivity,
        permissions: List<String>,
        hint: String = "即将重新申请的权限是程序必须依赖的权限",
        callback: PermissionXUtilsCallback
    ) {
        PermissionX.init(activity)
            .permissions(
                permissions
            )
            .onExplainRequestReason { scope, deniedList ->
                val permissionXCustomDialog =
                    PermissionXCustomDialog(activity, hint, deniedList)
                permissionXCustomDialog.setCancelable(true)
                permissionXCustomDialog.setCanceledOnTouchOutside(true)
                scope.showRequestReasonDialog(permissionXCustomDialog)
            }.onForwardToSettings { scope, deniedList ->
                val permissionXCustomDialog =
                    PermissionXCustomDialog(activity, "您需要去应用程序设置当中手动开启权限", deniedList)
                scope.showForwardToSettingsDialog(permissionXCustomDialog)
                callback.permissionCallback(false, mutableListOf(), deniedList)
            }.request { allGranted, grantedList, deniedList ->
                callback.permissionCallback(allGranted, grantedList, deniedList)
            }
    }

    /**
     * 先提醒用户要申请权限的目的(activity)
     */
    fun checkPermissBefore(
        fragment: Fragment,
        permissions: List<String>,
        hint: String = "即将重新申请的权限是程序必须依赖的权限",
        callback: PermissionXUtilsCallback
    ) {
        PermissionX.init(fragment)
            .permissions(
                permissions
            )
            .explainReasonBeforeRequest()
            .onExplainRequestReason { scope, deniedList ->
                val permissionXCustomDialog =
                    PermissionXCustomDialog(
                        fragment.requireContext(),
                        hint,
                        deniedList
                    )
                permissionXCustomDialog.setCancelable(true)
                permissionXCustomDialog.setCanceledOnTouchOutside(true)
                scope.showRequestReasonDialog(permissionXCustomDialog)
            }.onForwardToSettings { scope, deniedList ->
                val permissionXCustomDialog =
                    PermissionXCustomDialog(
                        fragment.requireContext(),
                        "您需要去应用程序设置当中手动开启权限",
                        deniedList
                    )
                scope.showForwardToSettingsDialog(permissionXCustomDialog)
            }.request { allGranted, grantedList, deniedList ->
                callback.permissionCallback(allGranted, grantedList, deniedList)
            }
    }

    /**
     * 直接申请权限（fragment)
     */
    fun checkPermiss(
        fragment: Fragment,
        permissions: List<String>,
        hint: String = "即将重新申请的权限是程序必须依赖的权限",
        callback: PermissionXUtilsCallback
    ) {
        PermissionX.init(fragment)
            .permissions(
                permissions
            )
            .onExplainRequestReason { scope, deniedList ->
                val permissionXCustomDialog =
                    PermissionXCustomDialog(
                        fragment.requireContext(),
                        hint,
                        deniedList
                    )
                permissionXCustomDialog.setCancelable(true)
                permissionXCustomDialog.setCanceledOnTouchOutside(true)
                scope.showRequestReasonDialog(permissionXCustomDialog)
            }.onForwardToSettings { scope, deniedList ->
                val permissionXCustomDialog =
                    PermissionXCustomDialog(
                        fragment.requireContext(),
                        "您需要去应用程序设置当中手动开启权限",
                        deniedList
                    )
                scope.showForwardToSettingsDialog(permissionXCustomDialog)
            }.request { allGranted, grantedList, deniedList ->
                callback.permissionCallback(allGranted, grantedList, deniedList)
            }
    }

    // 检查权限
    fun hasPermission(activity: Activity, permission: String): Boolean {
        return ContextCompat.checkSelfPermission(
            activity,
            permission
        ) == PackageManager.PERMISSION_GRANTED
    }

    fun checkPermissionCameraRead(
        activity: FragmentActivity,
        hint: String = "请授予相机、存储权限，以便使用相机拍摄或在手机相册中选择图片",
        callback: PermissionXUtilsCallback
    ) {
        val permissions = mutableListOf(
            Manifest.permission.CAMERA,
//            Manifest.permission.RECORD_AUDIO,

        )
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            permissions.add(Manifest.permission.READ_MEDIA_IMAGES)
//            permissions.add(Manifest.permission.READ_MEDIA_AUDIO)
//            permissions.add(Manifest.permission.READ_MEDIA_VIDEO)
        } else {
            permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE)
        }
        checkPermissBefore(
            activity,
            permissions,
            hint,
            callback
        )
    }

}

interface PermissionXUtilsCallback {

    fun permissionCallback(
        allGranted: Boolean,
        grantedList: List<String?>,
        deniedList: List<String?>
    )
//    fun fotPermsission()
}
